package com.ufo.store.management.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.ufo.store.management.R;
import com.ufo.store.management.entity.Isszmbalv3PayFlow;
import com.ufo.store.management.entity.Isszmbalv3PayFlowGroup;

import java.util.List;

/**
 *  作者：taoli on 2017/3/1 0001 13:53
 *  
 */
public class Isszmbalv3PayFlowExpandAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater = null;
    private List<Isszmbalv3PayFlowGroup> groupList;
    private Context mContext;

    /**
     * 构造方法
     *
     * @param context
     * @param group_list
     */
    public Isszmbalv3PayFlowExpandAdapter(Context context,List<Isszmbalv3PayFlowGroup> group_list) {
        this.groupList = group_list;
        this.mContext = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * 返回一级Item总数
     */
    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return groupList.size();
    }

    /**
     * 返回二级Item总数
     */
    @Override
    public int getChildrenCount(int groupPosition) {
        if (groupList.get(groupPosition).getChildList() == null) {
            return 0;
        } else {
            return groupList.get(groupPosition).getChildList().size();
        }
    }

    /**
     * 获取一级Item内容
     */
    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupList.get(groupPosition);
    }

    /**
     * 获取二级Item内容
     */
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).getChildList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {

        GroupViewHolder holder = new GroupViewHolder();

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.group_status_item, null);
        }
        holder.groupName = (TextView) convertView
                .findViewById(R.id.one_status_name);

        String hour = groupList.get(groupPosition).getGroupName();
        if(Integer.parseInt(hour) > 12 ){
            holder.groupName.setText("下午"+String.valueOf(Integer.parseInt(hour) - 12)+"点");
        }else{
            holder.groupName.setText("上午"+hour+"点");
        }

        setGroupAnimation(convertView,groupPosition);

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        Isszmbalv3PayFlow entity = (Isszmbalv3PayFlow) getChild(groupPosition,
                childPosition);
        if (convertView != null) {
            viewHolder = (ChildViewHolder) convertView.getTag();
        } else {
            viewHolder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.child_status_item, null);
            viewHolder.twoStatusTime = (TextView) convertView
                    .findViewById(R.id.two_complete_time);
        }
        if( ( groupList.get(groupPosition).getChildList().size() -1 )== childPosition ){
            viewHolder.twoStatusTime.setBackgroundResource(R.drawable.group_item_selector1);
        }else{
            viewHolder.twoStatusTime.setBackgroundResource(R.drawable.group_item_selector0);
        }
        viewHolder.twoStatusTime.setText("账单***"+entity.payFlowFlow_no.substring(6,16) + "  " +entity.payFlowOpertime+ "  交易额¥"+entity.payFlowPay_amount);

        convertView.setTag(viewHolder);

        setChildAnimation(convertView,groupPosition,childPosition);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

    private class GroupViewHolder {
        TextView groupName;
    }

    private class ChildViewHolder {
        public TextView twoStatusTime;
    }

    /**
     * @desc 自定义动画
     * @author taoli
     * @time 2017/6/9 0009
     */
    private int lastGroupPosition = -1;
    private void setGroupAnimation(View viewToAnimate, int positionGroup) {
        if (positionGroup > lastGroupPosition) {
            Animation translateAnimation=new TranslateAnimation(0, 0, 100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastGroupPosition = positionGroup;
        }else if( positionGroup <  lastGroupPosition ){
            Animation translateAnimation=new TranslateAnimation(0, 0, -100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastGroupPosition = positionGroup;
        }
    }

    /**
     * @desc 自定义动画
     * @author taoli
     * @time 2017/6/9 0009
     */
    private int lastChildPosition = -1;
    private void setChildAnimation(View viewToAnimate, int positionGroup, int childPosition) {
        int pos = -1;
        if( positionGroup == 0 ){
            pos = childPosition;
        }else{
            for( int i = positionGroup -1; i >= 0 ; i-- ){
                pos = pos + groupList.get(positionGroup -1).getChildList().size();
            }
            pos = pos + childPosition;
        }
        if (pos > lastChildPosition) {
            Animation translateAnimation=new TranslateAnimation(0, 0, 100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastChildPosition = pos;
        }else if( pos <  lastChildPosition ){
            Animation translateAnimation=new TranslateAnimation(0, 0, -100, 0);
            translateAnimation.setDuration(700);
            translateAnimation.setInterpolator(new OvershootInterpolator());
            viewToAnimate.setAnimation(translateAnimation);
            lastChildPosition = pos;
        }
    }
}
